package xuegao.study.mall.rbac.controller;

import com.alibaba.excel.EasyExcel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import xuegao.study.mall.common.api.CommonResult;
import xuegao.study.mall.common.model.ums.UmsDepartment;
import xuegao.study.mall.common.vo.ums.UmsDepartmentVo;
import xuegao.study.mall.rbac.service.UmsDepartmentService;
import xuegao.study.mall.rbac.util.DepartmentDataListener;
import xuegao.study.mall.rbac.util.UmsDepartmentExcelInfo;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Api(tags = "UmsDepartmentController")
@Tag(name = "UmsDepartmentController", description = "部门管理相关接口")
@RestController
@RequestMapping(value = "/department")
public class UmsDepartmentController {

    @Autowired
    private UmsDepartmentService umsDepartmentService;

    @Autowired
    private ApplicationContext applicationContext;

    @ApiOperation("部门保存")
    @PostMapping(value = "/save")
    public CommonResult<String> save(@RequestBody UmsDepartment department) {
        int count = umsDepartmentService.save(department);
        if (count > 0) {
            return CommonResult.success("ok");
        }
        return CommonResult.failed();
    }

    @ApiOperation("部门删除")
    @DeleteMapping(value = "/delete/{id}")
    public CommonResult<Boolean> delete(@PathVariable Long id) {
        int count = umsDepartmentService.deleteById(id);
        if (count > 0) {
            return CommonResult.success(Boolean.TRUE);
        }
        return CommonResult.failed();
    }

    @ApiOperation("部门修改")
    @PostMapping(value = "/update")
    public CommonResult<Boolean> update(@RequestBody UmsDepartment department) {
        int count = umsDepartmentService.update(department);
        if (count > 0) {
            return CommonResult.success(Boolean.TRUE);
        }
        return CommonResult.failed();
    }

    @ApiOperation("部门查询->属性列表")
    @GetMapping(value = "/list")
    public CommonResult<List<UmsDepartmentVo>> list() {
        List<UmsDepartmentVo> voList = umsDepartmentService.listAll();
        return CommonResult.success(voList);
    }

    @ApiOperation("部门导出")
    @GetMapping(value = "/export")
    public void export(HttpServletResponse response) throws IOException {
        umsDepartmentService.export(response);
    }

    @ApiOperation("部门导入")
    @PostMapping(value = "/import")
    public CommonResult<Boolean> importDep(@RequestParam("file")MultipartFile file) {
        try {
            EasyExcel.read(file.getInputStream(), UmsDepartmentExcelInfo.class, new DepartmentDataListener(applicationContext))
                    .sheet()
                    .doRead();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return null;
    }
}
